import axios from '@/utils/request.js'
import { getStorage } from '@/utils/storage'
// 获取所有频道列表
export const getAllChannelListApi = () =>
  axios({
    url: '/v1_0/channels',
    method: 'get'
  })
// 登录的api
export const loginApi = (value) => axios({
  url: '/v1_0/authorizations',
  method: 'post',
  data: value
})
// 用户刷新token
export const getNewTokenApi = () => axios({
  url: '/v1_0/authorizations',
  method: 'put',
  headers: { // axios请求拦截器统一携带的是token，而这次请求需要带的是refresh_token
    // 所以在axios请求拦截器里做判断，就是为了这种情况准备的
    Authorization: `Bearer ${getStorage('refresh_token')}`
  }
})
// 关注用户
export const userFollowedApi = ({ userId }) => axios({
  url: '/v1_0/user/followings',
  method: 'post',
  data: {
    target: userId
  }
})
// 取消关注用户
export const userUnFollowedApi = ({ userId }) => axios({
  url: `/v1_0/user/followings/${userId}`,
  method: 'DELETE'
})
// 获取用户频道列表
export const getUserChannelsListApi = () => axios({
  url: '/v1_0/user/channels'
})
// 获取用户的基本信息
export const getUserInfoApi = () => axios({
  url: '/v1_0/user'
})
// 获取用户的个人信息 编辑信息用
export const getUserProfileApi = () => axios({ // profile 用户资料
  url: '/v1_0/user/profile'
})
// 更新用户头像
// 知识点
// Content-Type: application/json; axios携带的，前提: data请求体是对象 -> json字符串 -> 发送给后台
// Content-Type: multipart/form-data; 浏览器携带的，前提: data请求体必须是FormData类型对象
export const updateUserPhoto = (fd) => axios({
  url: '/v1_0/user/photo',
  method: 'PATCH',
  data: fd // 表单对象
})
// 编辑用户资料
export const updateUserProfileApi = (dataObj) => {
  const obj = {
    name: '', // 用户名
    gerder: 0, // 性别 0：男 1：女
    birthday: '', // 生日(要求格式年-月-日)
    intro: '' // 个人介绍
  }
  for (const prop in obj) {
    if (dataObj[prop] === undefined) { // 用obj里的key去外面传入的参数对象中匹配，如果没找到(证明外面没有传这个参数)
      delete obj[prop] // 从obj上移除这对属性和值
    } else {
      obj[prop] = dataObj[prop] // 如果找到了，就从外面对象取出对应的key值，保存到obj上
    }
  }
  return axios({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: obj
  })
}
// 获取文章列表
export const getAllArticleList = ({ channel_id, timestamp }) => axios({
  url: '/v1_0/articles',
  method: 'get',

  params: {
    channel_id,
    timestamp
  }
})
// 对文章点赞
export const likeArticelApi = ({ artId }) => axios({
  url: '/v1_0/article/likings',
  method: 'post',
  data: {
    target: artId
  }
})
// 对文章取消点赞
export const unLikeArticelApi = ({ artId }) => axios({
  url: `/v1_0/article/likings/${artId}`,
  method: 'delete'
})
// 对文章评论喜欢
export const commentLikeApi = ({ comId }) => axios({
  url: '/v1_0/comment/likings',
  method: 'post',
  data: {
    target: comId
  }
})
// 对文章评论取消喜欢
export const commentDislikeApi = ({ comId }) => axios({
  url: `/v1_0/comment/likings/${comId}`,
  method: 'delete'
})
// 获取文章评论
export const getArticleComments = ({ id, offset = null, limit = 10 }) => axios({ // axios只针对params参数，当键值对值为空时会忽略此参数名和值不携带在url？后面
  url: '/v1_0/comments',
  method: 'get',
  params: {
    type: 'a', // 不做评论回复功能所以写固定的a
    source: id,
    offset,
    limit
  }
})
// 对文章进行评论-发布评论
export const commentSendApi = ({ id, content, art_id = null }) => {
  // axios只能处理忽略params中的null(有值为空就不携带给后台)data就不行需要我们自己处理
  const obj = {
    target: id,
    content
  }
  if (art_id !== null) { // 为true表示是对评论进行回复需要弟三个参数，反之则是对文章进行评论不需要第三个参数
    obj.art_id = art_id
  }
  return axios({
    url: '/v1_0/comments',
    method: 'post',
    data: obj
  })
}
// 更新频道列表
export const updateChannelsApi = (channels) => axios({
  url: '/v1_0/user/channels',
  method: 'put',
  data: {
    channels
  }
})
// 删除频道列表
export const deleteUserChannelApi = (channelId) => axios({
  url: `/v1_0/user/channels/${channelId}`,
  method: 'delete'
})
// 对文章不喜欢
export const dislikeArticle = (artID) => axios({
  url: '/v1_0/article/dislikes',
  method: 'POST',

  data: {
    target: artID
  }
})
// 反馈垃圾内容
export const reportArticleApi = ({ artID, type }) => axios({
  url: '/v1_0/article/reports',
  method: 'post',

  data: {
    target: artID,
    type,
    remark: '垃圾内容'
  }
})
// 获取联想建议
export const getSuggestListApi = ({ keyWords }) => axios({
  url: '/v1_0/suggestion',
  params: {
    q: keyWords
  }
})
// 获取搜索结果页
export const getSearchResultsApi = ({ page = 1, per_page = 10, q }) => axios({
  url: '/v1_0/search',
  params: {
    page,
    per_page,
    q
  }
})
// 获取新闻详情
export const getNewsDetailApi = ({ art_id }) => axios({ // art_id 文章id
  url: `/v1_0/articles/${art_id}`,
  method: 'get'
})
